﻿using UnityEngine;
using System.Collections;

public class EggCheck : MonoBehaviour {

	//Transform textTrans;
	Transform eggTrans;
	//TextMesh textMesh;
	bool HasCollided = false;
	public float FadeTime = 0.25f;
	float StartFade = 0f;
	// Use this for initialization
	void Start () {
		StartFade = FadeTime;
		eggTrans = this.transform;
		//textTrans = eggTrans.FindChild ("EggText").transform;
		//textMesh = (TextMesh)textTrans.gameObject.GetComponent (typeof(TextMesh));
	}
	
	// Update is called once per frame
	void Update () {
		if (this.HasCollided) {
			FadeTime -= Time.deltaTime;
			if (FadeTime >= 0f) {
				Color col = this.gameObject.renderer.material.color;
				col.a = Mathf.Lerp(0f, 1f, FadeTime * (1f / StartFade));
				this.gameObject.renderer.material.color = col;
				this.gameObject.transform.localScale *= 1.01f;

			}
			else {
				GameObject.Destroy(this.eggTrans.gameObject);
			}
		}
	}

	void FixedUpdate() {
		//textMesh.text = eggTrans.gameObject.rigidbody2D.velocity.ToString ();
	}

	void OnCollisionEnter2D(Collision2D other) {
		if (other.collider.tag == "Player") {
			//Debug.Log("Got to collision");
			CircleCollider2D col = (CircleCollider2D)this.gameObject.GetComponent(typeof(CircleCollider2D));
			col.enabled = false;
			LifeCountDown cDown = (LifeCountDown)this.gameObject.GetComponent(typeof(LifeCountDown));
			GameMaintainerSingleton.Instance.TouchedItem(cDown.LifeTime);
			this.HasCollided = true;
		}
	}
}
